﻿using System;
using System.Collections.Generic;
using System.Text;
using Oragon.Architecture.Data.SqlBuilder;

namespace Oragon.Architecture.Data.QueryBuilder.SqlServer
{
	public class SQLServerLiteralBuilder : ILiteralBuilder
	{
		#region ILiteralBuilder Members

		public SqlLiteral BuildTableName(params string[] args)
		{
			if (args.Length == 1)
				args = args[0].Split('.');

			return new SqlLiteral(string.Concat("[", string.Join("].[", args), "]"));
		}

		public SqlLiteral BuildColumnName(params string[] args)
		{
			if (args.Length == 1)
				args = args[0].Split('.');
			return new SqlLiteral(string.Concat("[", string.Join("].[", args), "]"));
		}

		public SqlLiteral BuildInt(int value)
		{
			return new SqlLiteral(value.ToString());
		}
		public SqlLiteral BuildDate(DateTime value)
		{
			return new SqlLiteral(value.ToString("yyyy-MM-dd"));
		}
		
		public SqlLiteral BuildDateTime(DateTime value)
		{
			return new SqlLiteral(value.ToString("yyyy-MM-dd HH:mm:ss"));
		}

		public SqlLiteral BuildParameter(string parameterName)
		{
			return new SqlLiteral(string.Concat("@", parameterName));
		}

		public OrderByItem BuildOrderBy(string projectionColumn, Sorting sorting)
		{
			return new OrderByItem(string.Concat("[", projectionColumn, "]"), sorting);
		}

		public ISQLStatement GetLastIdentity(string columnName)
		{
			if (string.IsNullOrEmpty(columnName))
				columnName = "NewIdentity";
			string SQL = string.Format("SELECT SCOPE_IDENTITY() as {0} ;", this.BuildColumnName(columnName).ToSQL());
			return new SqlLiteral(SQL);
		}

		#endregion
	}
}
